<?php


namespace Attendance\Controller;

use Think\Controller;


class IndexPCController extends Controller
{
    //考勤统计
    public function principalindex()
    {   
        //设置UTF-8编码格式
        header("Content-type: text/html;charset=utf-8");

        //查询关键字进行搜索。
        if(I('nickname')){    
            $nickname = I('nickname');
            $map['nickname'] = array('like','%'.$nickname.'%');
            $this->assign('nickname',$nickname);
        }

        //获取角色
        $group_id = $_SESSION['oc_home']['group'];
        
        //学校、班级类型
        if($group_id == "6"){
            $con['pid'] = $_SESSION['oc_home']['unit'];
            $this->units = M('core_unit')->where($con)->select();
            $this->classlist=M('core_class')->where('unit='.$_SESSION['oc_home']['unit'])->select();
        }else{
            $con['id'] = $_SESSION['oc_home']['unit'];
            $this->units = M('core_unit')->where($con)->select();
            $this->classlist=M('core_class')->where('unit='.$_SESSION['oc_home']['unit'])->select();
        }

        //查询考勤记录表条件
        $map['unit'] = I('unit');
        $map['class_id'] = I('class_id');

        //根据时间段查询
        if(I('create_time')){
            $map['create_time'] = array(between,array(strtotime(I('create_time')),strtotime(I('create_time').'23:59:58')));
        }
        else{
            //默认状态只查询当天的出勤状态
            $map['create_time'] = array('gt',strtotime(date('Y-m-d 00:00:00')));
        }


        $params = array(
            'unit'=>I('unit'),
            'class_id'=>I('class_id'),
            'create_time'=>I('create_time'),
            'go'=>I('go')
            
        );
        $this->assign('params',$params);
        //查询应到出勤人数
        $this->count = M('core_attendance_detail')->where($map)->count();
        //查询缺勤人数
        $map1['status'] = array('neq',1);
        $this->miss_num = M('core_attendance_detail')->where($map)->where($map1)->count();

        //分页查询
        if(I('go') == "2"){
            $map2['status']   = array('neq',1);
            $totalCount = M('core_attendance_detail')->where($map)->where($map2)->count();
            $page = new \Think\Page($totalCount,10,$params);
            $limit = $page->firstRow . ',' . $page->listRows . ',' . $page->totalRows;
            $list=M('core_attendance_detail')->where($map)->where($map2)->limit($limit)->order('id desc')->select();
        }else{
            $totalCount = M('core_attendance_detail')->where($map)->count();
            $page = new \Think\Page($totalCount,10,$params);
            $limit = $page->firstRow . ',' . $page->listRows . ',' . $page->totalRows;
            $list=M('core_attendance_detail')->where($map)->limit($limit)->order('id desc')->select();
        }
        //查询学生缺勤状态
        foreach ($list as $key => $value) {
            $list[$key]['uid'] = M('core_userinfo_view')->where('uid='.$value['uid'])->getField('nickname');
            $list[$key]['unit'] = M('core_unit')->where('id='.$value['unit'])->getField('title');
            $list[$key]['class_id'] = M('core_class')->where('id='.$value['class_id'])->getField('title');
        }
        $this->assign('_page',$page->show());
        $this->assign('list',$list);
        $this->assign('go',I('go'));
        $this->display();
    }   

}
